# -*- coding: utf-8 -*-
# Author: xiantingDeng
# File: read_util.py
# Time: 19:26


import os
import csv
import xlrd
import json
import yaml
import configparser
from common.utils.file_util import get_data_file_path
from common.utils.file_util import get_real_ini_dir,get_data_yaml


class OperationCsv():
    def __init__(self, filename):
        self.file_path = os.path.join(get_data_file_path(filename))

    def get_csv_data(self):
        # 读取csv文件
        with open(self.file_path, "r", encoding="utf-8")as fp:
            data_list = []
            for i in csv.reader(fp):
                data_list.append(i)
        # data = "".join(data_list[-1])
        return data_list

    def transform_data(self, noe):
        # 读取csv具体到哪一行数据且转化为字符串
        # 以0开头的索引
        data = "".join(self.get_csv_data()[noe])
        return data

    def statistice_data(self):
        # 统计csv文件里面有多少数据
        with open(self.file_path, "r", encoding="utf-8")as fp:
            return len(fp.readlines())


class OperationExcel():
    def __init__(self, filename):
        self.file_path = os.path.join(get_data_file_path(filename))

    def get_xlsx_data(self, sheet_name):
        '''
        打开Excel文件且读取sheet，如果该Excel文件中第一行第一列
        文本不是“接口名称”就append加入list中
        :param sheet_name：sheet值
        :return:返回整个Excel操作对象
        '''
        data_list = []
        xls_path = self.file_path
        with xlrd.open_workbook(xls_path) as fp:
            sheet = fp.sheet_by_name(sheet_name)
            nrows = sheet.nrows
            for i in range(nrows):
                data_list.append(sheet.row_values(i))
        return data_list

    def get_lines_data(self, sheet_name):
        '''
        统计Excel总行数
        :param sheet_name:sheet值
        :return: 返回Excel有多少行数据
        '''
        xls_path = self.file_path
        file = xlrd.open_workbook(xls_path)
        sheet = file.sheet_by_name(sheet_name)
        rows = sheet.nrows
        return rows

    def get_value_data(self, sheet_name, row, col):
        '''
        获取Excel指定数据
        :param sheet_name:sheet名称
        :param row: 行
        :param col: 列
        :return: 返回获取的数据
        '''
        return self.get_xlsx_data(sheet_name)[row][col]


class OperationIni():
    def __init__(self, filename):
        self.file_path = get_real_ini_dir(filename)

    def get_ini_file_data(self):
        '''
        读取ini配置文件所有节点信息
        :param file_path: ini配置文件下的绝对路径
        :return: 返回ini文件中所有节点信息，当文件不存在或没有数据时返回空字典：{}
        '''
        config = configparser.ConfigParser()
        config.read(self.file_path, encoding="utf-8")
        dict_data = dict()
        for item in config.items():
            temp_dict = dict()
            if item[0] == "DEFAULT":
                continue
            line = config.items(item[0])
            temp_dict[item[0]] = dict(line)
            dict_data.update(temp_dict)
        return dict_data

    def get_ini_file_path_section_data(self, section_name):
        '''
        读取ini配置文件指定节点信息
        :param section_name:节点名称
        :return:返回ini文件中指定节点信息，当文件不存在或没有数据时返回空字典：{}
        '''
        config = configparser.ConfigParser()
        config.read(self.file_path, encoding="utf-8")
        dict_data = {}
        if config.has_section(section_name):
            options = config.options(section_name)
            for option in options:
                kv_dict = {}
                item = config.get(section_name, option)
                kv_dict[option] = item
                dict_data.update(kv_dict)
            return dict_data
        else:
            return {}

    def get_ini_option_data(self, section_name, option_name):
        '''
        获取配置文件下制定节点下制定键的值
        :param file_path:ini配置文件绝对路径
        :param section_name:节点名称
        :param option_name:节点下键值
        :return:返回指定节点下指定键的值，当文件不存在或节点或键不存在时返回None
        '''
        config = configparser.ConfigParser()
        config.read(self.file_path, encoding="utf-8")
        if config.has_section(section_name):
            if config.has_option(section_name, option_name):
                value = config.get(section_name, option_name)
                return value
        else:
            return None


class OperationJson():
    def __init__(self, filename):
        self.file_path = get_data_file_path(filename)

    def get_json_data(self):
        '''
        读取json文件,查看文件内所有内容
        :return: 返回该文件读取方式
        '''
        with open(self.file_path, encoding="utf-8") as fp:
            data = json.load(fp)
        return data

    def get_json_datas(self, key):
        '''
        获取json数据
        :param key: json的key值
        :return: 返回json数据
        '''
        data = self.get_json_data()
        return data[key]

class OperationYaml():
    def __init__(self,filename):
        self.file_path = get_data_yaml(filename)

    def get_yaml(self):
        '''
        读取yaml文件数据
        :return:以字典方式返回
        '''

        with open(self.file_path, "r", encoding="utf-8") as f:
            file_content = f.read()
        content = yaml.load(file_content, yaml.FullLoader)
        return content

    def get_annotation(self,key):
        '''
        获取yaml文件中的注解
        :param key: key值
        :return: 返回注解
        '''
        return self.get_yaml().get(key).get("annotation")


if __name__ == '__main__':
    print(OperationYaml("new_theme.yaml").get_annotation("zhutimingcheng"))
